package org.dromara.erp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.erp.domain.ErpGoodsInOutInfo;
import org.dromara.erp.service.IErpGoodsInOutInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 进出明细
 *
 * @author Mr Lei
 * @date 2024-06-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/erp/goodsInOutInfo")
public class ErpGoodsInOutInfoController extends BaseController {

    private final IErpGoodsInOutInfoService erpGoodsInOutInfoService;

    /**
     * 查询进出明细列表
     */
    @SaCheckPermission("erp:goodsInOutInfo:list")
    @GetMapping("/list")
    public TableDataInfo<ErpGoodsInOutInfo> list(ErpGoodsInOutInfo bo, PageQuery pageQuery) {
        return erpGoodsInOutInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出进出明细列表
     */
    @SaCheckPermission("erp:goodsInOutInfo:export")
    @Log(title = "进出明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpGoodsInOutInfo bo, HttpServletResponse response) {
        List<ErpGoodsInOutInfo> list = erpGoodsInOutInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "进出明细", ErpGoodsInOutInfo.class, response);
    }

    /**
     * 获取进出明细详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("erp:goodsInOutInfo:query")
    @GetMapping("/{id}")
    public R<ErpGoodsInOutInfo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(erpGoodsInOutInfoService.queryById(id));
    }

    /**
     * 新增进出明细
     */
    @SaCheckPermission("erp:goodsInOutInfo:add")
    @Log(title = "进出明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpGoodsInOutInfo bo) {
        return toAjax(erpGoodsInOutInfoService.insert(bo));
    }

    /**
     * 修改进出明细
     */
    @SaCheckPermission("erp:goodsInOutInfo:edit")
    @Log(title = "进出明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpGoodsInOutInfo bo) {
        return toAjax(erpGoodsInOutInfoService.update(bo));
    }

    /**
     * 删除进出明细
     *
     * @param ids 主键串
     */
    @SaCheckPermission("erp:goodsInOutInfo:remove")
    @Log(title = "进出明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(erpGoodsInOutInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
